package bat.iog.hytool.tomcat.startup;

import iog.hytool.tomcat.container.Server;
import iog.hytool.tomcat.core.LifecycleException;

import java.io.File;

public class Catalina {
    
    public Catalina() {
    }
    
    //Prevent duplicate loads.
    protected boolean loaded = false;
    
    public static final String SERVER_XML = "tomcat/conf/server.xml";
    //Pathname to the server configuration file.
    protected String configFile = SERVER_XML;
    //Use await.
    protected boolean await = false;
    //The server component we are starting or stopping.
    protected Server server = null;
    
    //----------------------------------get和set方法start--------------------------------------
    public void setAwait(boolean b) {
        await = b;
    }
    
    public boolean isAwait() {
        return await;
    }
    
    public void setServer(Server server) {
        this.server = server;
    }
    
    public Server getServer() {
        return server;
    }
    //----------------------------------get和set方法end--------------------------------------
    
    public void load() {

        //只执行一次
        if (loaded) {
            return;
        }
        loaded = true;
        
        long t1 = System.nanoTime();
        
        //系统属性设置
        //initNaming();
        
        // Parse main server.xml
        //创建Digester对象，向其中添加conf/server.xml文件的解析规则，解析conf/server.xml文件
        parseServerXml(true);
        Server s = getServer();
        if (s == null) {
            return;
        }

        try {
            getServer().init();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
        
    }
    //解析server.xml文件
    protected void parseServerXml(boolean b) {
        String property = System.getProperty("user.dir");//文件目录
        System.out.println(property);
        //获取文件对象
        File file = configFile();
    }

    
    private File configFile() {
        File file = new File(configFile);
        if (!file.isAbsolute()) {
            file = new File(System.getProperty("user.dir"), configFile);
        }
        return file;
    }
}
